package validators

import (
	"fmt"
	"github.com/pkg/errors"
	"github.com/thedevsaddam/govalidator"
	"regexp"
)

func init() {
	govalidator.AddCustomRule("tel", func(field string, rule string, message string, value interface{}) error {
		re := regexp.MustCompile(`^1[3-9](\d){9}$`)
		val := value.(string)
		res := re.FindString(val)
		if res == "" {
			if message != "" {
				return errors.New(message)
			}
			return fmt.Errorf("%v格式错误", val)
		}
		return nil
	})
}
